package com.xgkj.diyiketang.activity.data;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.xgkj.diyiketang.ProtocolConst;
import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.data.AnchorInfo;
import com.xgkj.diyiketang.data.GameInfo;
import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.util.PalLog;
import com.xgkj.diyiketang.util.ViewUtil;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by XCKJ_Paladowow on 2016/3/18.
 */
public class XiaoYuanBangAdapter extends BaseAdapter {
    private List<GameInfo> mList = new ArrayList<GameInfo>();
    private Context mContext;
    public XiaoYuanBangAdapter(Context context) {
        mContext = context;
    }
    public void updateData(List<GameInfo> list){
        if(mList != null){
            mList.addAll(list);
        }
        notifyDataSetChanged();
    }



    public void clearData(){
        if(mList != null){
            mList.clear();
        }

        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if(mList != null){
            return mList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if(mList != null){
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = new ViewHolder();
        if (convertView == null || convertView.getTag() ==null) {
            convertView = LayoutInflater.from(this.mContext).inflate(
                    R.layout.xiaoyuanbang_item, null, false);
//            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//            params.setMargins(ViewUtil.dip2px(mContext,10),ViewUtil.dip2px(mContext,10),ViewUtil.dip2px(mContext,10),ViewUtil.dip2px(mContext,10));
//            convertView.setLayoutParams(params);
//            convertView.setBackgroundResource(R.drawable.xiaoyuan_bg);
            holder.xyName = (TextView) convertView
                    .findViewById(R.id.xiaoyuanname);
//            holder.anchorList = (ListView) convertView
//                    .findViewById(R.id.cansaizhelist);
            holder.stateImg = (ImageView) convertView.findViewById(R.id.zhuangtai_img);
            holder.listLayout = (LinearLayout) convertView.findViewById(R.id.list_layout);
//            holder.anchorList.setEnabled(true);
//            holder.anchorList.setFocusableInTouchMode(false);
//            holder.anchorList.setFocusable(false);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final GameInfo vpInfo = mList.get(position);
        List<AnchorInfo> gridListData = vpInfo.getList();
        //PalLog.e("数量", gridListData.size() + "");
        String state = vpInfo.getGameState();
        if (state.equals("0")){
            holder.stateImg.setBackgroundResource(R.drawable.baomingzhong);
        }else if(state.equals("1")){
            holder.stateImg.setBackgroundResource(R.drawable.jinxingzhong);
        }else{
            holder.stateImg.setBackgroundResource(R.drawable.yijieshu);
        }
        holder.xyName.setText(vpInfo.getGameName());
        holder.listLayout.removeAllViews();
        for (int i=0;i<3;i++){
            View v = LayoutInflater.from(mContext).inflate(R.layout.xiaoyuanbang_anchor_item, null);
            ImageView zhuboImg = (ImageView) v.findViewById(R.id.xybanchor_icon);
            TextView zhuboNick = (TextView) v.findViewById(R.id.xybanchor_nick);
            TextView zhuboSign = (TextView) v.findViewById(R.id.xybanchor_sign);
            TextView fansNum = (TextView) v.findViewById(R.id.xybanchor_fansnum);
            TextView paiming = (TextView) v.findViewById(R.id.xybanchor_num);
            TextView zhuboCode = (TextView) v.findViewById(R.id.xybanchor_code);
            if (i < gridListData.size()) {
                AnchorInfo info = gridListData.get(i);
                if (info != null) {
                    String anchorImgUrl = info.getAnchorLogo();
                    if (anchorImgUrl != null && anchorImgUrl.length() > 0) {
                        ImageCache.getInstance(mContext).loadBitmapToView(zhuboImg, anchorImgUrl, new ViewUtil.Size(ViewUtil.dip2px(mContext, 40), ViewUtil.dip2px(mContext, 40)));
                    }
                    if (info.getNickName() != null) {
                        try {
                            zhuboNick.setText(URLDecoder.decode(info.getNickName(), "utf-8"));
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                    }
                    if (info.getAnchorSign() != null) {
                        try {
                            zhuboSign.setText(URLDecoder.decode(info.getAnchorSign(), "utf-8"));
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                    }
                    fansNum.setText("人气："+info.getAnchorVote());
                    zhuboCode.setText("NO." + info.getAnchorCode());
                }
            } else {
                zhuboNick.setText("虚位以待");
            }

            if ( i== 0) {
                paiming.setBackgroundResource(R.drawable.top1);
            } else if (i == 1) {
                paiming.setBackgroundResource(R.drawable.top2);
            } else if (i == 2) {
                paiming.setBackgroundResource(R.drawable.top3);
            }
           holder.listLayout.addView(v);
        }
//        if (holder.anchorList != null) {
//
//            final XYBAnchorAdatper gridViewAdapter = new XYBAnchorAdatper(mContext);
//            holder.anchorList.setAdapter(gridViewAdapter);
//            gridViewAdapter.updateData(gridListData);
//
//        }
        return convertView;
    }

    private class ViewHolder {
        TextView xyName;
        ImageView stateImg;
//        ListView anchorList;
        LinearLayout listLayout;

    }
}
